/*The MIT License

Copyright (c) <2008> <Samir Menon>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#ifndef CNEURONALNETWORK_H_
#define CNEURONALNETWORK_H_

#include "CSpikeSpooler.h"
//Only uses neurons with synapses
#include "CSynCaKNaNeuron.h"
#include "CDCaKNaNeuron.h"

namespace bis_neuron
{

#define NUM_STDP_NEURONS 800
#define NUM_DA_NEURONS 80
#define NUM_CTX_OUTBOUND_CONN 2
#define NUM_BG_OUTBOUND_CONN 2
#define NUM_CTX_SYNAPSES 2
#define NUM_BG_SYNAPSES 20
#define NUM_CYCLES 10000

//Custom cycles for each simulation
#define NUM_CYCLES_SIMPLENEURON 3000
#define NUM_CYCLES_KNANEURON 1000

using namespace std;

class SCtxNeuron
{
	friend class CNeuronalNetwork;
	CSynCaKNaNeuron* stdp_neuron;
	unsigned int conn_to[NUM_CTX_OUTBOUND_CONN];
	
	SCtxNeuron()
	{
		stdp_neuron = new CSynCaKNaNeuron(NUM_CTX_SYNAPSES);
		for(int i=0;i<NUM_CTX_OUTBOUND_CONN;i++)
			{	conn_to[i] = -1;	}
	}
	~SCtxNeuron()
	{
		if(NULL!=stdp_neuron){delete stdp_neuron;	}
	}
};

class SBgNeuron
{
	friend class CNeuronalNetwork;
	CDCaKNaNeuron* da_neuron;
	unsigned int conn_to[NUM_BG_OUTBOUND_CONN];
	
	SBgNeuron()
	{
		da_neuron = new CDCaKNaNeuron(NUM_BG_SYNAPSES);
		for(int i=0;i<NUM_BG_OUTBOUND_CONN;i++)
			{	conn_to[i] = -1;	}
	}
	~SBgNeuron()
	{
		if(NULL!=da_neuron){delete da_neuron;	}
	}
};

class CNeuronalNetwork
{
	protected:
	SCtxNeuron *ctx_neurons;	
	SBgNeuron *bg_neurons;	
	unsigned int num_ctx_neurons, num_bg_neurons;
	unsigned int ctx_per_bg;
	
	//Network parameters:
	CSystemClock *network_clock;
	CSpikeSpooler *sp_spool;
	CSpikeSpooler *data_spool;
	float r, sigma, n;
	float tau_k, delta_gk;
	float tau_ca, r_ca, delta_i_ca;
	
public:
	CNeuronalNetwork(const unsigned int & arg_num_ctx_neurons = NUM_STDP_NEURONS,
									 const unsigned int & arg_num_bg_neurons = NUM_DA_NEURONS,
									 const string & arg_spike_outfilename = "",
									 const string & arg_data_outfilename = "");
	
	//Part of the ctx
	//Each neuronal sim has its own initialize-run function set
	//1. The cortex-bg loop
	void run_ctx2bg();
	void initialize_ctx2bg();
	
	//2. A simple bistability simulation
	void run_bgbistable();
	void initialize_bgbistable();	
	
	//3. A simple cubic neuron
	void initialize_simpleneuron(const float& arg_r);
	void run_simpleneuron();
	
	//4. A simple K-Na neuron
	void initialize_knaneuron(const float& arg_tau_k, 
														const float& arg_delta_gk);
	void run_knaneuron();
	
	//5. A simple K-Na neuron
	void initialize_caknaneuron(const float& arg_tau_ca, 
														const float& arg_delta_i_ca);
	void run_caknaneuron();
	
	//6. A bistable Ca-K-Na neuron
	void initialize_bistableneuron();
	void run_bistableneuron();
	
	virtual ~CNeuronalNetwork();
};

}

#endif /*CNEURONALNETWORK_H_*/
